package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Student;
import com.itheima.service.Impl.UserServiceImpl;
import com.itheima.service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends BasicSevlet {
    //创建JSON工具对象
    ObjectMapper mapper = new ObjectMapper();
    //创建业务层对象
    UserService service = new UserServiceImpl();

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取账号和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //System.out.println(username + "," + password);
        //验证账号密码
        boolean hasUser = service.login(username, password);
        //用户是否存在
        if(hasUser){
            //将用户保存到会话域
            req.getSession().setAttribute("username",username);
            //响应数据
            resp.getWriter().write("true");
        }else {
            //响应数据
            resp.getWriter().write("false");
        }


    }


    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取当前页和最大显示
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        //调用业务层查询方法
        Page page = service.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        //System.out.println(list);

        //封装PageInfo
        PageInfo info = new PageInfo(page);
        System.out.println(info);

        //转json  响应给前端
        String json = mapper.writeValueAsString(info);
        resp.getWriter().write(json);

    }
}
